{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 处理商家的信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "os_train=pd.read_csv(r'D:\\Data\\TCForNewComer\\deal\\valid\\os_train_shop.csv')\n",
    "test=pd.read_csv(r'D:\\Data\\TCForNewComer\\deal\\Same_userid\\oc_test.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3697\n",
      "1559\n",
      "1230\n"
     ]
    }
   ],
   "source": [
    "col='Merchant_id'\n",
    "a=set(os_train[col].values)\n",
    "b=set(test[col].values)\n",
    "print(len(a))\n",
    "print(len(b))\n",
    "print(len(a&b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# for id in (a-b):\n",
    "#     os_train.drop(list(os_train[os_train['Merchant_id']==id].index),axis=0,inplace=True)\n",
    "os_train=os_train.sort_values(by=['User_id']).copy()\n",
    "os_train.index=np.arange(0,len(os_train),1)\n",
    "test.index=np.arange(0,len(test),1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "test['shop_id']=0\n",
    "for indtest in np.arange(0,len(test),1):\n",
    "    indtrain=os_train[os_train['User_id']==test.loc[indtest,'User_id']].index\n",
    "    testdis=test.loc[indtest,'Distance']\n",
    "    if len(indtrain)==0:\n",
    "        continue\n",
    "    else:\n",
    "        if testdis not in os_train.loc[indtrain,'Distance'].values:\n",
    "            test.loc[indtest,'shop_id']=-1\n",
    "            continue\n",
    "        else:\n",
    "            ot=os_train.loc[indtrain]\n",
    "            test.loc[indtest,'shop_id']=ot[ot['Distance']==testdis]['shop_id'].max()   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "i=3000\n",
    "tet=test[test['Discount_rate']<1]\n",
    "for disc in set(tet['Discount_rate'].values):\n",
    "    test.loc[tet[tet['Discount_rate']==disc].index,'shop_id']=i\n",
    "    i+=1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "i=test['shop_id'].max()\n",
    "for id in set(test[test['shop_id']==-1]['User_id'].values):\n",
    "    tt=test[(test['shop_id']==-1)&(test['User_id']==id)]\n",
    "    test.loc[tt.index,'shop_id']=i\n",
    "    i=i+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "osss_train=pd.read_csv(r'D:\\Data\\TCForNewComer\\deal\\Same_userid\\oc_train.csv')\n",
    "osss_test=pd.read_csv(r'D:\\Data\\TCForNewComer\\deal\\Same_userid\\oc_test.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {
    "collapsed": true,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "indx=osss_train[osss_train['Discount_rate']<1].index\n",
    "discount=osss_train.loc[indx]\n",
    "osss_train=osss_train.drop(indx,axis=0)\n",
    "discount.index=np.arange(0,len(discount),1)\n",
    "osss_train.index=np.arange(0,len(os_train),1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "discount['shop_id']=0\n",
    "i=3000\n",
    "for disc in set(discount['Discount_rate'].values):\n",
    "    discount.loc[discount[discount['Discount_rate']==disc].index,'shop_id']=i\n",
    "    i+=1\n",
    "os_train=pd.concat([os_train,discount],axis=0).copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "os_train.index=np.arange(0,len(os_train),1)\n",
    "test.index=np.arange(0,len(test),1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "os_train.to_csv(r'D:\\Data\\TCForNewComer\\deal\\Same_userid\\shop\\or_train.csv',index=False)\n",
    "test.to_csv(r'D:\\Data\\TCForNewComer\\deal\\Same_userid\\shop\\or_test.csv',index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "os_train['num_pc']=0          #店铺发放的优惠券张数       6月份\n",
    "os_train['num_M100']=0        #店铺发放大于100的优惠券张数\n",
    "os_train['num_L100']=0          #店铺发放小于100的优惠券张数\n",
    "os_train['num_M100use']=0         #店铺大于100优惠券被使用的张数\n",
    "os_train['num_L100use']=0        #店铺小于100优惠券被使用的张数\n",
    "os_train['num_use']=0            #店铺优惠券总的被使用的张数\n",
    "os_train['use_pro']=0            #使用占得比率\n",
    "os_train['cus_shop_num']=0     #同一个用户在同一个店铺领取的张数\n",
    "os_train['cus_shop_use']=0    #同一个用户在同一个店铺使用的张数\n",
    "for id in set(os_train['shop_id'].values):\n",
    "    ot=os_train[os_train['shop_id']==id]\n",
    "    os_train.loc[ot.index,'num_pc']=len(ot)\n",
    "    if len(ot[ot['Discount_rate']>=100])!=0:\n",
    "        os_train.loc[ot[ot['Discount_rate']>=100].index,'num_M100']=len(ot[ot['Discount_rate']>=100])\n",
    "    if len(ot[ot['Discount_rate']<100])!=0:\n",
    "        os_train.loc[ot[ot['Discount_rate']<100].index,'num_L100']=len(ot[ot['Discount_rate']<100])\n",
    "    if len(ot[(ot['Discount_rate']>=100)&(ot['Date']==1)])!=0:\n",
    "        os_train.loc[ot[(ot['Discount_rate']>=100)&(ot['Date']==1)].index,'num_M100use']=len(ot[(ot['Discount_rate']>=100)&(ot['Date']==1)])\n",
    "    if len(ot[(ot['Discount_rate']<100)&(ot['Date']==1)])!=0:\n",
    "        os_train.loc[ot[(ot['Discount_rate']<100)&(ot['Date']==1)].index,'num_L100use']=len(ot[(ot['Discount_rate']<100)&(ot['Date']==1)])\n",
    "    if len(ot[ot['Date']==1])!=0:\n",
    "        os_train.loc[ot[ot['Date']==1].index,'num_use']=len(ot[ot['Date']==1])\n",
    "    os_train.loc[ot.index,'use_pro']=len(ot[ot['Date']==1])/len(ot)\n",
    "    for uid in set(ot['User_id'].values):\n",
    "        otu=os_train[(os_train['shop_id']==id)&(os_train['User_id']==uid)]\n",
    "        os_train.loc[otu.index,'cus_shop_num']=len(otu)\n",
    "        if len(otu[otu['Date']==1])!=0:\n",
    "            os_train.loc[otu[otu['Date']==1].index,'cus_shop_use']=len(otu[otu['Date']==1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {
    "collapsed": true,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "test['num_pc']=0          #店铺发放的优惠券张数\n",
    "test['num_M100']=0        #店铺发放大于100的优惠券张数\n",
    "test['num_L100']=0          #店铺发放小于100的优惠券张数\n",
    "test['num_M100use']=0         #店铺大于100优惠券被使用的张数\n",
    "test['num_L100use']=0        #店铺小于100优惠券被使用的张数\n",
    "test['num_use']=0            #店铺优惠券总的被使用的张数\n",
    "test['use_pro']=0            #使用占得比率\n",
    "test['cus_shop_num']=0     #同一个用户在同一个店铺领取的张数\n",
    "test['cus_shop_use']=0    #同一个用户在同一个店铺使用的张数\n",
    "for id in set(test['shop_id'].values):\n",
    "    ot=test[test['shop_id']==id]\n",
    "    test.loc[ot.index,'num_pc']=len(ot)\n",
    "    if len(ot[ot['Discount_rate']>=100])!=0:\n",
    "        test.loc[ot[ot['Discount_rate']>=100].index,'num_M100']=len(ot[ot['Discount_rate']>=100])\n",
    "    if len(ot[ot['Discount_rate']<100])!=0:\n",
    "        test.loc[ot[ot['Discount_rate']<100].index,'num_L100']=len(ot[ot['Discount_rate']<100])\n",
    "    if id in os_train['shop_id'].values:\n",
    "        ott=os_train[(os_train['shop_id']==id)]\n",
    "        if len(ott[(ott['Discount_rate']>=100)&(ott['Date']==1)])!=0:\n",
    "            test.loc[ot.index,'num_M100use']=ott[(ott['Discount_rate']>=100)&(ott['Date']==1)]['num_M100use'].max()\n",
    "        if len(ott[(ott['Discount_rate']<100)&(ott['Date']==1)])!=0:\n",
    "            test.loc[ot.index,'num_L100use']=ott[(ott['Discount_rate']<100)&(ott['Date']==1)]['num_M100use'].max()\n",
    "        test.loc[ot.index,'num_use']=len(ott[ott['Date']==1])\n",
    "        test.loc[ot.index,'use_pro']=len(ott[ott['Date']==1])/len(ott)\n",
    "    for uid in set(ot['User_id'].values):\n",
    "        otu=test[(test['shop_id']==id)&(test['User_id']==uid)]\n",
    "        otr=os_train[(os_train['shop_id']==id)&(os_train['User_id']==uid)]\n",
    "        test.loc[otu.index,'cus_shop_num']=len(otu)\n",
    "        test.loc[otu.index,'cus_shop_use']=len(otr[otr['Date']==1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "os_train['temp']=os_train['Date_received'].apply(lambda x: int(str(x)[0]))\n",
    "test['temp']=test['Date_received'].apply(lambda x: int(str(x)[0]))\n",
    "col=['num_pc','num_M100','num_L100','num_M100use','num_L100use','num_use']\n",
    "for id in set(os_train['shop_id'].values):\n",
    "    ot=os_train[os_train['shop_id']==id]\n",
    "    length=len(set(ot['temp'].values))\n",
    "    for i in col:\n",
    "        os_train.loc[ot.index,i]=os_train.loc[ot.index,i]/length\n",
    "for id in set(test['shop_id'].values):\n",
    "    ot=test[test['shop_id']==id]\n",
    "    length=len(set(ot['temp'].values))\n",
    "    for i in col:\n",
    "        test.loc[ot.index,i]=test.loc[ot.index,i]/length\n",
    "for j in col:\n",
    "    os_train[j]=os_train[j].apply(lambda x: int(x))\n",
    "    test[j]=test[j].apply(lambda x: int(x))\n",
    "os_train.drop(['temp'],axis=1,inplace=True)\n",
    "test.drop(['temp'],axis=1,inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['User_id', 'Merchant_id', 'Coupon_id', 'Discount_rate', 'Distance',\n",
       "       'Date_received', 'Date', 'ord_buy', 'Coupon_num', 'Coupon_nouse_num',\n",
       "       'Coupon_use_num', 'Coupon_use_prob', 'Coupon_greaterthan100',\n",
       "       'Coupon_lessthan100', 'Coupon_lep100', 'Coupon_grp100',\n",
       "       'Avg_saler_Coupon', 'Avg_distance', 'shop_count', 'shop_pro',\n",
       "       'Coupon_diffcount', 'Coupon_diffpro', 'same_Coupon_count', 'shop_id',\n",
       "       'num_pc', 'num_M100', 'num_L100', 'num_M100use', 'num_L100use',\n",
       "       'num_use', 'use_pro', 'cus_shop_num', 'cus_shop_use', 'temp'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "os_train.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2, 3, 4, 5, 6}"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "set(os_train['temp'].values)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "ot=os_train[(os_train['shop_id']==13)&(os_train['Date_received']>=600)]\n",
    "ot[ot['Date']==1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "os_train.to_csv(r'D:\\Data\\TCForNewComer\\deal\\Same_userid\\shop\\shop_train.csv',index=False)\n",
    "test.to_csv(r'D:\\Data\\TCForNewComer\\deal\\Same_userid\\shop\\shop_test.csv',index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "55129"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(test[test['shop_id']==13])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
